<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> UDT Reference</title>
<link rel="stylesheet" href="udtdoc.css" type="text/css" />
</head>

<body>
<div class="ref_head">&nbsp;UDT Reference: Functions</div>

<h4 class="func_name"><strong>getsockname</strong></h4>
<p>The <b>getsockname</b> method retrieves the local address associated with a UDT socket.</p>

<div class="code">int getsockname(<br />
&nbsp; UDTSOCKET <font color="#FFFFFF">u</font>,<br />
&nbsp; struct sockaddr* <font color="#FFFFFF">name</font>,<br />
&nbsp; int* <font color="#FFFFFF">namelen</font><br />
);</div>

<h5>Parameters</h5>
<dl>
  <dt><i>u</i></dt>
  <dd>[in] Descriptor identifying a connected socket.</dd>
  <dt><em>name</em></dt>
  <dd>[out] The structure to store the local address.</dd>
  <dt><em>addrlen</em></dt>
  <dd>[in, out] pointer to the size of the <i>name</i> structure.</dd>
</dl>

<h5>Return Value</h5>
<p>On success, <strong>getlasterror</strong> returns 0 and the local address information is stored in <i>name</i>; otherwise it returns UDT::ERROR and the specific error information can be 
retrieved using <a href="error.htm">getlasterror</a>.</p>

<table width="100%" border="1" cellpadding="1" cellspacing="0" bordercolor="#CCCCCC">
  <tr>
    <td width="17%" class="table_headline"><strong>Error Name</strong></td>
    <td width="17%" class="table_headline"><strong>Error Code</strong></td>
    <td width="83%" class="table_headline"><strong>Comment</strong></td>
  </tr>
  <tr>
    <td>EINVPARAM</td>
    <td>5003</td>
    <td>Invalid parameters.</td>
  </tr>
  <tr>
    <td>EINVSOCK</td>
    <td>5004</td>
    <td><i>u</i> is an invailid UDT socket.</td>
  </tr>
  <tr>
    <td>EUNBOUNDSOCK</td>
    <td>5005</td>
    <td><i>u</i> is not bound to a local address yet.</td>
  </tr>
</table>

<h5>Description</h5>
<p>The <strong>getsockname</strong> retrieves the local address associated with the socket. The UDT socket must be bound explicitly (via <strong>bind</strong>) or implicitly (via 
<strong>connect</strong>), otherwise this method will fail because there is no meaningful address bound to the socket.</p>
<p>If <strong>getsockname</strong> is called after an explicit <strong>bind</strong>, but before <strong>connect</strong>, the IP address returned will be exactly the IP address that is used for <strong>bind</strong> and it may be 0.0.0.0 if ADDR_ANY is used. If <strong>getsockname</strong> is called after <strong>connect</strong>, the IP address returned will be the address that the peer socket sees. In the case when there is a proxy (e.g., NAT), the IP address returned will be the translated address by the proxy, but not a local address. If there is no proxy, the IP address returned will be a local address. In either case, the port number is local (i.e, not the translated proxy port).</p>
<p>Because UDP is connection-less, using <u>getsockname</u> on a UDP port will almost always return 0.0.0.0 as IP address (unless it is bound to an explicit IP) . As a connection oriented protocol, UDT will return a meaningful IP address by <strong>getsockname</strong> if there is no proxy translation exist.</p>
<p>UDT has no multihoming support yet. When there are multiple local addresses and more than one of them can be routed to the destination address, UDT may not behave properly due to the multi-path effect. In this case, the UDT socket must be explicitly bound to one of the local addresses. </p>
<h5>See Also</h5>
<p><strong><a href="bind.htm">listen</a>, <a href="bind.htm">bind</a>, <a href="connect.htm">connect</a></strong></p>
<p>&nbsp;</p>

</body>
</html>
